package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.BeatiDescription;
import com.lanou.dllo.findfood.dialog.WebProgressDialog;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class BeautifulLastActivity extends Activity {
    private WebView BwebView;
    private TextView description, BlCount, title;
    private NetworkImageView img;
    private TextView time;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticvity_beautlast);
        initView();
        getId();
        initData();
    }

    private void initData() {
        final WebProgressDialog dialog = new WebProgressDialog(this, R.id.animaImgweb);
        dialog.show();
        String httpUrl = "http://apis.baidu.com/tngou/food/show";
        String httpArg = "id=" + id;
        String path = httpUrl + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                BeatiDescription all = gson.fromJson(response, BeatiDescription.class);
                if (all != null) {
                    dialog.dismiss();
                    //   description.setText(all.getKeywords());
                    BlCount.setText(all.getCount() + "");

                    String url = all.getImg();
                    String imgUrl = "http://tnfs.tngou.net/image/" + url;
                    getBitmap(imgUrl, img);
                    String httpString = all.getMessage();
                    String mimrType = "text/html";
                    String enconding = "utf-8";
                    BwebView.loadDataWithBaseURL("fake://not/needed", httpString, mimrType, enconding, "");
                    BwebView.setInitialScale(140);
                    title.setText(all.getTitle());


                }


            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();
                ;
            }
        }

        ) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);
    }

    private void initView() {
        BwebView = (WebView) findViewById(R.id.BwebView);
        description = (TextView) findViewById(R.id.BlkeyWords);
        BlCount = (TextView) findViewById(R.id.BlCount);
        img = (NetworkImageView) findViewById(R.id.BlImg);
        time = (TextView) findViewById(R.id.longtime);

    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    public void getId() {
        id = getIntent().getIntExtra("id", 4);
    }
}
